package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.execption.CommonError;
import com.xuecheng.base.execption.XueChengPlusException;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.model.po.TeachplanMedia;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @author ybl
 * @date 2023/3/3 9:44
 * @description 课程基本信息管理业务接口 实现类
 * @since version-1.0
 */
@Service
public class TeachplanServiceImpl implements TeachplanService {

    @Autowired(required = false)
    private TeachplanMapper teachplanMapper;

    @Autowired(required = false)
    private TeachplanMediaMapper teachplanMediaMapper;

    /**
     * @author ybl
     * @date 2023/3/4 17:48
     * @description 课程计划查询
     * @param courseId 课程id
     * @return java.util.List<com.xuecheng.content.model.dto.TeachplanDto>
     * @since version-1.0
     */
    @Override
    public List<TeachplanDto> findTeachplayTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    /**
     * @author ybl
     * @date 2023/3/4 17:48
     * @description 课程计划新增，修改
     * @param teachplan 课程计划数据
     * @since version-1.0
     */
    @Transactional
    @Override
    public void saveTeachplan(SaveTeachplanDto teachplan) {
        Long id = teachplan.getId();
        Teachplan t = teachplanMapper.selectById(id);
        if (t == null) {
            // 新增
            // 初始化对象
            t = new Teachplan();
            BeanUtils.copyProperties(teachplan, t);
            // 设置排序
            int count = teachplanMapper.selectMaxOrderby(t.getCourseId(), t.getParentid());
            t.setOrderby(count + 1);
            teachplanMapper.insert(t);
        } else {
            // 修改
            BeanUtils.copyProperties(teachplan, t);
            teachplanMapper.updateById(t);
        }
    }

    /**
     * @author ybl
     * @date 2023/3/6 9:02
     * @description 课程计划删除
     * @param id 课程计划id
     * @since version-1.0
     */
    @Transactional
    @Override
    public void deleteTeachplan(Long id) {
        // 先查询此计划类型，是否为章节，是，判断是否有小节. 如果有则返回错误信息
        Teachplan teachplan = teachplanMapper.selectById(id);
        if (teachplan.getParentid().equals(0L)) {
            // 章节，则查看是否有小节
            LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Teachplan::getParentid, id);
            List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
            if (!teachplans.isEmpty()) {
                // 有小节，返回错误信息
                XueChengPlusException.cast("课程计划信息还有子级信息，无法操作");
                return;
            }
            // 删除章节,逻辑删除
            teachplan.setStatus(0);
            teachplanMapper.deleteById(teachplan);
            return;
        }
        // 删除小节同时删除媒资信息数据
        LambdaQueryWrapper<TeachplanMedia> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TeachplanMedia::getTeachplanId, teachplan.getId());
        teachplanMediaMapper.delete(queryWrapper);
        teachplan.setStatus(0);
        teachplanMapper.deleteById(teachplan);
    }

    /**
     * @author ybl
     * @date 2023/3/6 11:37
     * @description 课程计划排序接口
     * @param move 移动类型
     * @param id 课程计划id
     * @since version-1.0
     */
    @Override
    public void moveTeachplan(String move, Long id) {
        // 根据id查询teachplan数据
        Teachplan teachplan = teachplanMapper.selectById(id);
        // 获取所有同级的数据
        LambdaQueryWrapper<Teachplan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Teachplan::getCourseId, teachplan.getCourseId())
                .eq(Teachplan::getParentid, teachplan.getParentid())
                .orderByAsc(Teachplan::getOrderby);
        List<Teachplan> teachplans = teachplanMapper.selectList(queryWrapper);
        // 需要返回的错误信息,
        if (teachplans.size() <= 1) {
            XueChengPlusException.cast("无法移动，因为是唯一数据");
            return;
        } else if (teachplans.indexOf(teachplan) == teachplans.size() - 1 && Objects.equals(move, "movedown")) {
            XueChengPlusException.cast("无法移动，因为是最末端");
            return;
        } else if (teachplans.indexOf(teachplan) == 0 && Objects.equals(move, "moveup")) {
            XueChengPlusException.cast("无法移动，因为是最前端");
            return;
        }
        // 根据move判断，执行与上或下Orderby值交换，来排序
        updateOrderby(teachplan, teachplans, move);
    }

    /**
     * @author ybl
     * @date 2023/3/6 16:29
     * @description 执行排序，上下移动
     * @param teachplan 操作的课程计划
     * @param teachplans 同级计划
     * @param move 移动类型
     * @since version-1.0
     */
    private void updateOrderby(Teachplan teachplan, List<Teachplan> teachplans, String move) {
        int index = teachplans.indexOf(teachplan);
        Teachplan teachplanUp;
        if (Objects.equals(move, "moveup")) {
            teachplanUp = teachplans.get(index - 1);
        } else if (Objects.equals(move, "movedown")) {
            teachplanUp = teachplans.get(index + 1);
        } else {
            XueChengPlusException.cast(CommonError.PARAMS_ERROR);
            return;
        }
        Integer temp = teachplan.getOrderby();
        teachplan.setOrderby(teachplanUp.getOrderby());
        teachplanUp.setOrderby(temp);
        teachplanMapper.updateById(teachplan);
        teachplanMapper.updateById(teachplanUp);
    }
}
